package com.jack.helloworld.activity;

import android.os.Bundle;
import android.widget.Toast;

import com.jack.helloworld.R;
import com.jack.helloworld.adapter.FragmentViewPageAdapter;
import com.jack.helloworld.fragment.FragmentView1;
import com.jack.helloworld.fragment.FragmentView2;
import com.jack.helloworld.fragment.FragmentView3;
import com.jack.helloworld.fragment.FragmentView4;

import java.util.ArrayList;
import java.util.List;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

public class FragmentViewPagerActivity extends AppCompatActivity implements ViewPager.OnPageChangeListener {

    List<Fragment> listFragment;
    List<String> listViewPagerTitle;

    ViewPager fragmentViewPager;
    FragmentViewPageAdapter fragmentViewPageAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fragment_view_pager);

        initView();
        initDate();
    }

    private void initView() {
        fragmentViewPager = (ViewPager) findViewById(R.id.fragmentViewPager);
    }

    private void initDate() {
        listFragment = new ArrayList<Fragment>();
        listViewPagerTitle = new ArrayList<String>();

        listFragment.add(new FragmentView1());
        listFragment.add(new FragmentView2());
        listFragment.add(new FragmentView3());
        listFragment.add(new FragmentView4());

        listViewPagerTitle.add("fragmentViewPagerTitle1");
        listViewPagerTitle.add("fragmentViewPagerTitle2");
        listViewPagerTitle.add("fragmentViewPagerTitle3");
        listViewPagerTitle.add("fragmentViewPagerTitle4");

        fragmentViewPageAdapter = new FragmentViewPageAdapter(getSupportFragmentManager(), listFragment, listViewPagerTitle);
        fragmentViewPager.setAdapter(fragmentViewPageAdapter);
        fragmentViewPager.addOnPageChangeListener(this);

    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        Toast.makeText(this, "fragment position:" + (position + 1), Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }
}
